using System;
using System.Xml;

using Scenario.WorkflowEngine.DataModel;

namespace Scenario.Modules.PCM.DataModel.ConfigurationManager.Rule
{
	//*********************************************************************
	//
	// XMLRules Class
	//
	// The Rule class represents a subworkflow to configure a selection sql
	// query or a selection calculate methotd for items selection from DB
	// This type of wf is used inside MetaWorkFlows and stored in scenario
	// workflows model.
	//
	//*********************************************************************

	public class XMLRule : bOS.XML.Data.XMLObject 
	{
		public const string MAIN_NODE = "Rule";

		private const string NAME_ATTRIBUTE		= "Name";
		private const string PRIORITY_ATTRIBUTE = "Priority";
		private const string DESCRIPTION_NODE	= "Description";

		/** FIELDS **/

		private int iPriority;
		private string sName;
		private string sDescription;

		private Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers vUsers;
		private Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles vRoles;
		private Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunctions vFunctions;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLRule()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLRule(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		/** PROPERTIES **/

		public string Name 
		{
			get { return sName; }
			set { sName = value; }
		}

		public int Priority 
		{
			get { return iPriority; }
			set { iPriority = value; }
		}

		public string Description 
		{
			get { return sDescription; }
			set { sDescription = value; }
		}

		public Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers XMLUsers
		{
			get { return vUsers; }
			set { vUsers = value; }
		}

		public Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles XMLRoles
		{
			get { return vRoles; }
			set { vRoles = value; }
		}

		public Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunctions XMLApplicationFunctions
		{
			get { return vFunctions; }
			set { vFunctions = value; }
		}
	
		/** INSTANCE METHODS **/

		/** Parsering method for filling the property **/
		override public void Init()
		{
			iPriority=0;
			Name= string.Empty;
			sDescription= string.Empty;

            vUsers = new Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers();
            vRoles = new Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles();

			vFunctions = new Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunctions();
		}

		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);
				switch ( node.Name )
				{
					case XMLRule.NAME_ATTRIBUTE:
						sName = node.InnerText;
						break;
					case XMLRule.PRIORITY_ATTRIBUTE:
						iPriority= Int32.Parse(node.InnerText);
						break;
				}
			}

			/** find the sub tags witch contains propery values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name)
				{
					case XMLRule.DESCRIPTION_NODE:
						sDescription = node.InnerText;
						break;
					case Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLUsers.MAIN_NODE:
						vUsers.Parse (node);
						break;
					case Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData.XMLRoles.MAIN_NODE:
						vRoles.Parse(node);
						break;
					case Scenario.WorkflowEngine.DataModel.WorkflowApplicationsData.XMLApplicationFunctions.MAIN_NODE:
						vFunctions.Parse(node);
						break;
				}
			}
		}

		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLRule.MAIN_NODE);
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute ( XMLRule.NAME_ATTRIBUTE, sName) );
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute ( XMLRule.PRIORITY_ATTRIBUTE, iPriority) );

			xmlNode.AddNode ( vRoles.GetXMLNode() );
			xmlNode.AddNode ( vUsers.GetXMLNode() );
			xmlNode.AddNode ( vFunctions.GetXMLNode() );

			xmlNode.AddNode ( new bOS.XML.XMLNode (XMLRule.DESCRIPTION_NODE, this.sDescription) );



			//TODO: WAPI.Workflow.XmlWorkflow workflow = this.MakeWorkflow("");
			//xmlNode.SetValue(workflow.GetXML());
			
			return xmlNode;
		}
	}
}